package org.anderground.editor.storage;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.anderground.core.Logger;
import org.anderground.modules.editor.importers.ModelImporter;


/**
 * @author Ilya Shurigyn (honorik@gmail.com)
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class ReadersRegistry {

	/**
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 */
	public static class FileFormat {

		private Class<? extends ModelImporter> importerClass;

		private String formatDescription;

		private String[] supportedExtensions;

		/**
		 * 
		 * @param importerClass
		 * @param shortFormatDescription
		 * @param fileExtensions
		 */
		private FileFormat(Class<? extends ModelImporter> importerClass,
				String shortFormatDescription, String[] fileExtensions) {
			this.importerClass = importerClass;
			this.formatDescription = shortFormatDescription;
			this.supportedExtensions = fileExtensions;
		}

		/**
		 * @return the readerClass
		 */
		public Class<? extends ModelImporter> getImporterClass() {
			return importerClass;
		}

		/**
		 * @return the formatDescription
		 */
		public String getFormatDescription() {
			return formatDescription;
		}

		/**
		 * @return the supportedExtensions
		 */
		public String[] getSupportedExtensions() {
			String[] extensions = new String[supportedExtensions.length];
			System.arraycopy(supportedExtensions, 0, extensions, 0,
					extensions.length);
			return extensions;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj instanceof FileFormat) {
				return importerClass.equals(((FileFormat) obj).importerClass);
			}
			return false;
		}

		@Override
		public int hashCode() {
			return importerClass.hashCode() + 1;
		}

	}

	private static Set<FileFormat> formats = new HashSet<FileFormat>();

	public static void registerFormat(
			Class<? extends ModelImporter> importerClass,
			String[] fileExtensions, String description) {
		Logger.i("registering new reader: " + description);
		formats.add(new FileFormat(importerClass, description, fileExtensions));
	}

	public static List<FileFormat> getFormats() {
		return new LinkedList<FileFormat>(formats);
	}

}
